package com.fudan.backend.dao;

import com.fudan.backend.handler.IdTypeHandler;
import com.fudan.backend.pojo.CartItem;
import com.fudan.backend.pojo.Commodity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CartDao {
    @Results(id = "cartResultMap", value ={
            @Result(column = "cart_item_id", property = "cartItemId", typeHandler = IdTypeHandler.class),
            @Result(column = "store_id", property = "storeId", typeHandler = IdTypeHandler.class),
            @Result(column = "user_id", property = "userId", typeHandler = IdTypeHandler.class),
            @Result(column = "commodity_id", property = "commodityId", typeHandler = IdTypeHandler.class),
            @Result(column = "commodity_id", property = "commodity",
                    javaType = Commodity.class,
                    one = @One(select = "com.fudan.backend.dao.CommodityDao.selectCommodityByCommodityId")),
    })
    @Select("select * from t_cart_item where cart_item_id = #{cartItemId}")
    CartItem selectCartItemByCartItemId(String cartItemId);

    @ResultMap("cartResultMap")
    @Select("select * from t_cart_item where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    List<CartItem> selectCartItemByUserId(String userId);

    @ResultMap("cartResultMap")
    @Select("<script>" +
            "select * from t_cart_item " +
            "where cart_item_id in " +
            "<foreach item='id' collection='cartItemIdList' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler}" +
            "</foreach>" +
            "</script>")
    List<CartItem> selectCartItemsByCartItemIdList(List<String> cartItemIdList);

    @ResultMap("cartResultMap")
    @Select("select * from t_cart_item where user_id = #{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler} " +
            "and commodity_id = #{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    CartItem selectCartItemByUserIdAndCommodityId(String userId, String commodityId);

    @Insert("insert into t_cart_item values " +
            "(#{cartItemId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{userId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}, " +
            "#{commodityNum}, #{status})")
    Integer insertCartItem(CartItem cartItem);

    @Delete("delete from t_cart_item " +
            "where cart_item_id = #{cartItemId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer deleteCartItemByCartItemId(String cartItemId);

    @Delete("<script>" +
            "delete from t_cart_item " +
            "where cart_item_id in " +
            "<foreach item='id' collection='cartItemIdList' open='(' close=')' separator=','>" +
            "#{id, typeHandler=com.fudan.backend.handler.IdTypeHandler}" +
            "</foreach>" +
            "</script>")
    Integer deleteCartItemsByCartItemIdList(List<String> cartItemIdList);

    @Update("update t_cart_item set " +
            "commodity_num = #{num} " +
            "where cart_item_id = #{cartItemId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer updateCommodityNumByCartItemId(String cartItemId, Integer num);

    /**
     * 将对应商品相关购物车设为失效
     * @param commodityId 商品id
     * @return 修改条数
     */
    @Update("update t_cart_item set " +
            "status = #{status} " +
            "where commodity_id = #{commodityId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer setStatusByCommodityId(String commodityId, Integer status);
    /**
     * 设置对应商店相关购物车状态
     */
    @Update("update t_cart_item set " +
            "status = #{status} " +
            "where store_id = #{storeId, typeHandler=com.fudan.backend.handler.IdTypeHandler}")
    Integer setStatusByStoreId(String storeId, Integer status);
}
